<?php
namespace Qy {
    /**
    * 企业微信主动发送消息类
    * @author Jamers
    * @since 2017.1.1
    * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E5%8F%91%E6%B6%88%E6%81%AF
    */
    class Message {
        private $common;
        
        public function __construct($ary = array()) {
            if (isset($ary['common'])) {
                $this->common = &$ary['common'];
            }else{
                $this->common = new \Qy\Common();
            }
        }
        /**
        * 主动给成员发送消息
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E5%8F%91%E6%B6%88%E6%81%AF
        * 
        * @param mixed $ary
        * @param AccessToken $a_token
        */
        public function sendMessage($ary = array(), $a_token = '') {
            if (empty($ary)) return false;
            if (!isset($ary['msgtype']) || !isset($ary['agentid'])) return false;
            if (!in_array($ary['msgtype'],$this->message_type)) return false;
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            $url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={$a_token}";
            return $this->common->httpRequest($url,$ary,'post');
        }
        
        
        /**
        * 生成XML明文消息
        * 
        * @param mixed $ary
        * @param mixed $type
        */
        public function GeneralXmlMessage($ary = array(), $type = 'text') {
            $msg = new \Qy\MessageBody();
            return $msg->GeneralMessage($ary,$type);
        }
    }
}
?>
